// API接口统一管理

import { get, post, put, del, upload } from '@/utils/request.js'

// 用户相关API
export const userApi = {
  // 用户登录
  login: (data) => post('/mobile/user/login', data, { loading: false }),
  
  // 获取用户信息
  getUserInfo: () => get('/mobile/user/info'),
  
  // 验证支付密码
  verifyPayPassword: (data) => post('/mobile/user/verifyPayPassword', data),
  
  // 修改支付密码
  updatePayPassword: (data) => post('/mobile/user/updatePayPassword', data),
  
  // 用户退出
  logout: () => post('/mobile/user/logout'),
  
  // 刷新token
  refreshToken: () => post('/mobile/user/refresh'),
  
  // 根据ID查询用户
  getUserById: (id) => get(`/mobile/user/${id}`)
};

// 服务相关API
export const maintenanceServiceApi = {
  // 查询服务列表（支持搜索）
  getServiceList: (params) => get('/mobile/service/search', params),
  
  // 根据ID查询服务详情
  getServiceDetail: (id) => get(`/mobile/service/${id}`),
  
  // 根据商户ID查询服务列表
  getServicesByMerchant: (merchantId) => get(`/mobile/service/merchant/${merchantId}`),
  
  // 根据服务类型查询服务列表
  getServicesByType: (typeId) => get(`/mobile/service/type/${typeId}`),
  
  // 获取热门服务
  getPopularServices: (limit = 10) => get('/mobile/service/popular', { limit }),
  
  // 根据价格区间查询服务
  getServicesByPriceRange: (params) => get('/mobile/service/price-range', params)
};

// 订单相关API（预留，根据实际需要添加）
export const maintenanceOrderApi = {
  // 创建订单
  createOrder: (data) => post('/mobile/order/create', data),
  
  // 获取订单列表
  getOrderList: (params) => get('/mobile/order/list', params),
  
  // 获取订单详情
  getOrderDetail: (id) => get(`/mobile/order/${id}`),
  
  // 取消订单
  cancelOrder: (id) => put(`/mobile/order/${id}/cancel`),
  
  // 删除订单
  deleteOrder: (id) => del(`/mobile/order/${id}`)
};

// 商户相关API
export const maintenanceMerchantApi = {
  // 获取商户列表
  getMerchantList: (params) => get('/mobile/merchant/list', params),
  
  // 获取商户详情
  getMerchantDetail: (id) => get(`/mobile/merchant/${id}`),
  
  // 查询附近门店列表
  getNearbyMerchants: (params) => get('/mobile/merchant/nearby', params),
  
  // 根据服务类型查询门店列表
  getMerchantsByServiceType: (typeId, params) => get(`/mobile/merchant/by-service-type/${typeId}`, params),
  
  // 获取门店评价列表
  getMerchantEvaluates: (id) => get(`/mobile/merchant/${id}/evaluates`),
  
  // 获取门店销量统计
  getMerchantSales: (id) => get(`/mobile/merchant/${id}/sales`)
};

// 评价相关API（预留）
export const maintenanceEvaluateApi = {
  // 获取评价列表
  getEvaluateList: (params) => get('/mobile/evaluate/list', params),
  
  // 创建评价
  createEvaluate: (data) => post('/mobile/evaluate/create', data)
};

// 车辆相关API
export const maintenanceCarApi = {
  // 查询用户车辆列表
  getUserCars: (userId) => get(`/mobile/car/user/${userId}`),
  
  // 查询用户默认车辆
  getUserDefaultCar: (userId) => get(`/mobile/car/user/${userId}/default`),
  
  // 根据ID查询车辆详情
  getCarInfo: (id) => get(`/mobile/car/${id}`),
  
  // 新增车辆
  addCar: (data) => post('/mobile/car', data),
  
  // 修改车辆信息
  updateCar: (data) => put('/mobile/car', data),
  
  // 删除车辆
  deleteCar: (id) => del(`/mobile/car/${id}`),
  
  // 设置默认车辆
  setDefaultCar: (userId, carId) => post('/mobile/car/set-default', { userId, carId })
};

// 优惠券相关API
export const maintenanceCouponApi = {
  // 获取用户在指定商家的可用优惠券（订单页面使用）
  getUserAvailableCouponsByMerchant: (merchantId) => get(`/mobile/userCoupon/my/available/${merchantId}`),
  
  // 获取用户所有优惠券
  getUserCoupons: () => get('/mobile/userCoupon/my/details'),
  
  // 获取用户可用优惠券
  getUserAvailableCoupons: () => get('/mobile/userCoupon/my/available'),
  
  // 领取优惠券
  receiveCoupon: (data) => post('/mobile/userCoupon/receive', data),
  
  // 使用优惠券
  useCoupon: (data) => post('/mobile/userCoupon/use', data),
  
  // 获取商家可用优惠券
  getMerchantCoupons: (merchantId) => get(`/mobile/coupon/available/${merchantId}`)
};

// 文件上传API
export const fileApi = {
  // 上传文件
  uploadFile: (filePath, formData = {}, options = {}) => 
    upload('/mobile/file/upload', filePath, formData, options)
};

// 消费记录相关API
export const consumptionApi = {
  // 获取所有消费记录
  getAllConsumptionRecords: () => get('/mobile/consumption-records/all'),
  
  // 获取用户消费记录
  getConsumptionRecords: (userId) => get('/mobile/consumption-records', { userId }),
  
  // 根据类型获取消费记录
  getConsumptionRecordsByType: (userId, consumptionType) => 
    get('/mobile/consumption-records/type', { userId, consumptionType }),
  
  // 获取用户消费统计
  getConsumptionStatistics: (userId) => get('/mobile/consumption-records/statistics', { userId })
};

// 团购相关API
export const groupPurchaseApi = {
  // 获取有效的团购活动列表
  getActiveGroupPurchases: () => get('/mobile/group-purchases/active'),
  
  // 获取团购详情
  getGroupPurchaseDetail: (id) => get(`/mobile/group-purchases/${id}`),
  
  // 参与团购
  joinGroupPurchase: (id, userId) => post(`/mobile/group-purchases/${id}/join`, { userId }),
  
  // 获取团购参与人数
  getGroupPurchaseParticipantCount: (id) => get(`/mobile/group-purchases/${id}/participants/count`)
};

// 优惠券相关API
export const couponApi = {
  // 获取所有优惠券列表
  getAllCoupons: () => get('/mobile/coupons'),
  
  // 获取可领取的优惠券列表
  getAvailableCoupons: () => get('/mobile/coupons/available'),
  
  // 根据商家ID获取优惠券列表
  getCouponsByMerchant: (merchantId) => get(`/mobile/coupons/merchant/${merchantId}`),
  
  // 领取优惠券
  receiveCoupon: (couponId, userId) => post(`/mobile/coupons/${couponId}/receive?userId=${userId}`),
  
  // 使用优惠券
  useCoupon: (couponId, userId) => post(`/mobile/coupons/${couponId}/use?userId=${userId}`),
  
  // 获取用户优惠券列表
  getUserCoupons: (userId) => get(`/mobile/user-coupons?userId=${userId}`)
};

// 加油站相关API
export const gasStationApi = {
  // 获取加油站列表
  getGasStations: () => get('/mobile/gas-stations'),
  
  // 根据经纬度查询附近加油站
  getNearbyGasStations: (latitude, longitude, radius = 5000) => 
    get('/mobile/gas-stations/nearby', { latitude, longitude, radius }),
  
  // 根据关键词搜索加油站
  searchGasStations: (keyword) => get('/mobile/gas-stations/search', { keyword }),
  
  // 获取加油站详情
  getGasStationDetail: (id) => get(`/mobile/gas-stations/${id}`)
};

// 消息中心相关API
export const messageApi = {
  // 获取用户消息列表
  getMessages: (userId) => get('/mobile/messages', { userId }),
  
  // 获取所有消息（用于测试显示所有数据）
  getAllMessages: () => get('/mobile/messages/all'),
  
  // 标记消息为已读
  markMessageAsRead: (id) => post(`/mobile/messages/${id}/read`),
  
  // 获取未读消息数量
  getUnreadMessageCount: (userId) => get('/mobile/messages/unread/count', { userId })
};

// 健康检查API
export const healthApi = {
  // 健康检查
  health: () => get('/mobile/health')
};

